package com.spr.gw.controller.admin;

import com.spr.gw.common.ResponseCode;
import com.spr.gw.common.ResultBean;
import com.spr.gw.entity.User;
import com.spr.gw.service.SystemLogService;
import com.spr.gw.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by lsy
 * on 2017/10/20 17:50.
 */
@Controller
@RequestMapping("/admin")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/loginSubmit")
    @ResponseBody
    public ResultBean loginUser(HttpServletRequest request){
        String vertifyCode = request.getParameter("vertifyCode");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        if(vertifyCode == null || !"0".equals(vertifyCode)){
            return ResultBean.createByCodeMessage(ResponseCode.VERTIFY_CODE_ERROR.getCode(),ResponseCode.VERTIFY_CODE_ERROR.getDesc());
        }
        User user = User.builder().username(username).password(password).build();
        return userService.login(user, request);
    }

    @RequestMapping(value = "/logout")
    @ResponseBody
    public ResultBean logout(HttpServletRequest request){
        return userService.logout(request);
    }
}
